﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyClass {
	public partial class AddProblemsForm : Form {
		private List<TextBox> choices;
		private List<Label> labels;
		private Controller controller;
		public AddProblemsForm(Controller c, int selectedIndex = -1) {
			InitializeComponent();

			controller = c;
			choices = new List<TextBox>();
			labels = new List<Label>();
			textBox2.Text = "4";
			comboBox1.Items.AddRange(controller.GetAllTasksName());
			if (selectedIndex != -1) comboBox1.SelectedIndex = selectedIndex;
			UpdateChoices();
		}

		private void textBox2_Leave(object sender, EventArgs e) {
			UpdateChoices();
		}

		private void textBox2_KeyUp(object sender, KeyEventArgs e) {
			if (e.KeyCode == Keys.Enter) {
				UpdateChoices();
			}
		}

		private void UpdateChoices() {
			int nChoices = Int32.Parse(textBox2.Text);
			if (nChoices == choices.Count) return;
			for (int i = 0; i < nChoices; ++i) {
				if (i >= choices.Count()) {
					TextBox choiceContent = new TextBox();
					choiceContent.Width = 220;
					choiceContent.Text = "";
					choiceContent.Location = new Point(30, i * 30 + 7);
					choices.Add(choiceContent);
					Label labelContent = new Label();
					labelContent.Text = ((char)('A' + i)).ToString() + ":";
					labelContent.Location = new Point(10, i * 30 + 10);
					labels.Add(labelContent);

					panel1.Controls.Add(choiceContent);
					panel1.Controls.Add(labelContent);
				}
			}
			for (int i = nChoices; i < choices.Count; ++i) {
				choices.ElementAt(i).Dispose();
			}
			for (int i = nChoices; i < labels.Count; ++i) {
				labels.ElementAt(i).Dispose();
			}
			choices.RemoveRange(nChoices, choices.Count - nChoices);
			labels.RemoveRange(nChoices, labels.Count - nChoices);
		}

		private void button1_Click(object sender, EventArgs e) {
			if (ReportProblem()) {
				textBox2.Text = "4";
				textBox3.Text = "";
				UpdateChoices();
				foreach (TextBox tb in choices) {
					tb.Text = "";
				}
				textBox1.Clear();
				radioButton1.Select();
			}
		}

		private void button2_Click(object sender, EventArgs e) {
			if (ReportProblem())
				Close();
		}

		private bool ReportProblem() {
			if (comboBox1.SelectedItem == null) {
				MessageBox.Show("No task selected!");
				return false;
			}
			string answers = textBox3.Text;
			//check s

			List<string> choicesStr = new List<string>();
			foreach (TextBox tb in choices) {
				choicesStr.Add(tb.Text);
			}
			controller.AddProblem(textBox1.Text, choicesStr.ToArray<string>(), answers, comboBox1.SelectedItem.ToString(), radioButton1.Checked);
			return true;
		}

		private void AddProblemsForm_Load(object sender, EventArgs e) {

		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {

		}

		private void button3_Click(object sender, EventArgs e) {
			Close();
		}

		private void button1_Click_1(object sender, EventArgs e) {
			if (ReportProblem()) {
				textBox2.Text = "4";
				textBox3.Text = "";
				UpdateChoices();
				foreach (TextBox tb in choices) {
					tb.Text = "";
				}
				textBox1.Clear();
				radioButton1.Select();
			}
		}
	}
}
